<?php
require_once 'include/db.php';
require_once 'include/setup.php'; 

$post = $_POST;
if (!isset($post['dispath'])) $post['dispath'] = null;

switch($post['type']){
    case 'r':
        if ($post['username']) {
            modifyOrRemovePrivileges($post['username'], $post['repo'], $post['path'], $post['level'], $post['type'], 'user',$config ,$post['dispath']);

            if($post['auth'] != 0) addOrUpdatePrivileges($post['username'], $post['repo'], $post['path'], $post['auth'], 'user', $config);
        }

        if ($post['groupname']) {
            modifyOrRemovePrivileges($post['groupname'], $post['repo'], $post['path'], $post['level'], $post['type'], 'group', $config ,$post['dispath']);
            if($post['auth'] != 0) addOrUpdatePrivileges($post['groupname'], $post['repo'], $post['path'], $post['auth'], 'group', $config);
        }
        rebuildAccessFile($config);
        break;

    case 'w':
        if ($post['username']) {
            modifyOrRemovePrivileges($post['username'], $post['repo'], $post['path'], $post['level'], $post['type'], 'user',$config ,$post['dispath']);

            if($post['auth'] != 0) addOrUpdatePrivileges($post['username'], $post['repo'], $post['path'], $post['auth'], 'user', $config);
        }

        if ($post['groupname']) {
            modifyOrRemovePrivileges($post['groupname'], $post['repo'], $post['path'], $post['level'], $post['type'], 'group', $config ,$post['dispath']);
            if($post['auth'] != 0) addOrUpdatePrivileges($post['groupname'], $post['repo'], $post['path'], $post['auth'], 'group', $config);
        }
        rebuildAccessFile($config);
        break;
    
    case 'unlinked':
        addOrRemoveUG($post['name'], $post['name2'], $post['type'], $post['option'], $config);
        rebuildAccessFile($config);
        break;

    case 'linked':
        addOrRemoveUG($post['name'], $post['name2'], $post['type'], $post['option'], $config);
        rebuildAccessFile($config);
        break;

    case 'vdate':
        if ($post['username']) updateVDate($post['username'], $post['repo'], $post['path'], $post['vdate'], 'user', $config);
        if ($post['groupname']) updateVDate($post['groupname'], $post['repo'], $post['path'], $post['vdate'], 'group', $config);
        rebuildAccessFile($config);
        break;

    case 'add':
        if ($post['username']) {
            userAddOrUpdate($post['username'], $post['isadmin'], $config, $post['password']);
        }
        if ($post['groupname']) {
            groupAdd($post['groupname'] , $config);
        }
        if ($post['reponame']) {
            repoAdd($post['reponame'] , $post['desc'], $config);
            rebuildAccessFile($config);
        }
        break;

    case 'del':
        if ($post['username']) {
            userOrGroupDelete($post['username'], 'user', $config);
        }
        if ($post['groupname']) {
            userOrGroupDelete($post['groupname'], 'group', $config);
        }
        if ($post['reponame']) {
            repoDelete($post['reponame'], $config);
        }
        rebuildAccessFile($config);
        break;
}

?>

